package com.atguigu.lease.web.admin.controller.system;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {
    @Autowired
    private SystemUserService systemUserService;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        IPage<SystemUserItemVo> page = new Page<>(current, size);
        systemUserService.customPage(page, queryVo);
        return Result.ok(page);
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        SystemUserItemVo systemUserItemVo = systemUserService.customGetById(id);
        return Result.ok(systemUserItemVo);
    }

    /**
     * 保存或更新后台用户信息
     * <p>
     * 此方法用于处理后台用户的保存或更新请求它首先检查用户密码是否提供，
     * 如果提供，则对密码进行MD5加密以确保安全性然后，调用服务层方法saveOrUpdate
     * 来保存或更新用户信息如果操作失败，返回失败的结果；否则，返回成功结果
     *
     * @param systemUser 包含用户信息的请求体，用于保存或更新用户信息
     * @return 操作结果，成功或失败
     */
    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        // 检查用户密码是否为空，如果不为空，则进行MD5加密
        if (!ObjectUtils.isEmpty(systemUser.getPassword())) {
            String md5Hex = DigestUtils.md5Hex(systemUser.getPassword());
            systemUser.setPassword(md5Hex);
        }
        // 调用服务层方法保存或更新用户信息
        boolean b = systemUserService.saveOrUpdate(systemUser);
        // 根据操作结果返回相应的结果
        if (!b) {
            return Result.fail();
        }
        return Result.ok();
    }


    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        LambdaQueryWrapper<SystemUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemUser::getUsername, username);
        boolean exists =systemUserService.count(wrapper)==0;
        return Result.ok(exists);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        boolean remove = systemUserService.removeById(id);
        if (!remove) {
            return Result.fail();
        }
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        LambdaUpdateWrapper<SystemUser> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(SystemUser::getId, id);
        wrapper.set(SystemUser::getStatus, status);
        boolean update = systemUserService.update(wrapper);
        if (!update){
            return Result.fail();
        }
        return Result.ok();
    }
}
